var dateUtils = require('../../../utils/dateUtil.js'); 
const app = getApp() 
Page({ 
 
  /** 
   * 页面的初始数据 
   */ 
  data: { 
    // switchList:[ 
    //   {title:'进行中',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},startTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:0}, 
    //   {title:'已结束',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},startTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:1}, 
    //   {title:'已发布',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},startTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:2} 
    // ], 
    dataList:[],
    selectIndex:0, 
    startTime:'', 
    endTime:'', 
 
    totalPage:0, 
    pageNo:1, 
    pageSize:10, 
    isLoading:false, 
    allloaded: false, 
 
    isRefresh: 0 
  }, 
 
  /** 
   * 生命周期函数--监听页面加载 
   */ 
  onLoad: function (options) { 
    wx.setNavigationBarTitle({ 
      title: "跳绳任务" 
    }) 
    this.showDate(); 
    this.getData(); 
  }, 
 
  getData(cd){ 
    //var vt = this.data.switchList[this.data.selectIndex] 
    var data = { 
      startTime:this.data.startTime, 
      endTime:this.data.endTime, 
     // type:vt.type, 
      pageNo:this.data.pageNo, 
      pageSize:this.data.pageSize, 
    } 
    console.log("任务列表参数="+JSON.stringify(data)); 
    wx.showLoading({ 
      title: '数据加载中...', 
      icon: 'none', 
    }) 
    this.setData({ 
      isLoading:true 
    }) 
    var that = this; 
    app.api.queryTaskRopeList(data).then(rest => { 
      wx.hideLoading() 
      this.setData({ 
        isLoading:false 
      }) 
      cd && cd() 
      if(rest&&rest.success){    
        console.log('列表数据='+JSON.stringify(rest)) 
        var myDataList = [...that.data.dataList,...rest.data.records]  
         
        that.setData({ 
          dataList:myDataList, 
          totalPage:rest.data.total 
        }) 


        // var vst  =  that.data.switchList; 
        // var dataList = [...vst[that.data.selectIndex].data.list,...rest.data.list] 
        // vst[that.data.selectIndex].data =rest.data 
        // vst[that.data.selectIndex].data.list = dataList 
         
        // that.setData({ 
        //   switchList:vst, 
        //   totalPage:rest.data.totalCount 
        // }) 
        // console.log('列表个数数据='+this.data.switchList[that.data.selectIndex].data.list.length) 
        //console.log('列表数据='+JSON.stringify(this.data.switchList)) 
      }else{ 
        wx.showToast({ 
          title: rest.errMsg||'网络异常,请重试！', 
          icon: 'none', 
          duration: 2000, 
          mask:true 
        }) 
      } 
    }) 
    .catch(r => { 
      console.error(r) 
      wx.showToast({ 
        title: '网络异常,请重试！', 
        icon: 'none', 
        duration: 2000, 
        mask:true 
      }) 
    }) 
  }, 
 
  showDate(){ 
    var starTiem = dateUtils.mathChangeDate(dateUtils.formatDataTime(new Date()),"-",7,true,'') 
    var dateCom =  this.selectComponent("#date-select"); 
    dateCom.onShowAlert(starTiem+" 00:00:00",null,['发布任务']); 
 
    var endTime = dateCom.data.endTimeStr  
    var startTime = dateCom.data.startTimeStr ; 
    // var list = this.data.switchList; 
    // for(var i=0;i<list.length;i++){ 
    //   var item = list[i]; 
    //   item.startTime = startTime 
    //   item.endTime  = endTime 
    // } 
    this.setData({ 
 //     switchList:list, 
      startTime:startTime, 
      endTime:endTime 
    }) 
 
     console.log("时间 switchList=="+this.data.startTime+"==="+this.data.endTime); 
  }, 
   
  //tab页面切换 
  // didClickIndex(e){ 
    
  //   let index = e.detail; 
  //   var listData = this.data.switchList; 
  //   listData[index].data.list = [] 
  //   console.log("=====didClickIndex="+index); 
  //   this.setData({ 
  //       selectIndex:index, 
  //       pageNo:1, 
  //       switchList:listData, 
  //       totalPage:0 
  //   }) 
  //   this.getData() 
  // }, 
 
  //发布任务 
  titleDateIndex(data){ 
     
    wx.navigateTo({ 
      url: '/packJumpTask/teacher/releaseTask/releaseTask?isEdit=1', 

    }) 
  }, 
  
  //结束时间按钮确认 
  bindTimeChangeEnd(val){ 
    let repTime = val.detail.replace(/-/g, '/'); 
    let timeTamp = Date.parse(repTime);  
    let starTime = Date.parse(this.data.startTime);  
    console.log(timeTamp+' Endpicker发送选择改变，携带值为'+ val.detail+"==="+starTime) 
    if(timeTamp>=starTime){ 
      // var list = this.data.switchList; 
      // for(var i=0;i<list.length;i++){ 
      //   var item = list[i]; 
      //   item.endTime = val.detail 
      // } 
      
      //list[this.data.selectIndex].data.list = [] 
 
      this.setData({ 
        //switchList:list, 
        endTime:val.detail, 
        pageNo:1, 
        dataList:[], 
        totalPage:0 
      }) 


      //console.log("End确认时间End==="+JSON.stringify(this.data.switchList)) 
      this.getData() 
    }else{ 
      console.log("时间 switchList=="+this.data.startTime+"==="+this.data.endTime); 
      var dateCom =  this.selectComponent("#date-select"); 
      dateCom.onShowAlert(this.data.startTime,this.data.endTime,['发布作业']); 
      wx.showToast({ 
        title: '结束时间需大于开始时间', 
        icon: 'none', 
        duration: 1500, 
        mask:true 
      }) 
    } 
  }, 
 
  //开始时间按钮确认 
  bindTimeChangeStart(val){ 
    let repTime = val.detail.replace(/-/g, '/'); 
    let timeTamp = Date.parse(repTime);  
    let endTime = Date.parse(this.data.endTime);  
    console.log(timeTamp+' Startpicker发送选择改变，携带值为'+ val.detail+"==="+endTime) 
    if(timeTamp<=endTime){ 
      // var list = this.data.switchList; 
      // for(var i=0;i<list.length;i++){ 
      //   var item = list[i]; 
      //   item.startTime = val.detail 
      // } 
      // list[this.data.selectIndex].data.list = [] 
      this.setData({ 
       // switchList:list, 
        startTime:val.detail, 
        pageNo:1, 
        dataList:[], 
        totalPage:0 
      }) 
      //console.log("Start确认时间Start==="+JSON.stringify(this.data.switchList)) 
      this.getData() 
    }else{ 
      //console.log("时间 switchList=="+this.data.startTime+"==="+this.data.endTime); 
      var dateCom =  this.selectComponent("#date-select"); 
      dateCom.onShowAlert(this.data.startTime,this.data.endTime,['发布任务']); 
      wx.showToast({ 
        title: '开始时间需小于结束时间', 
        icon: 'none', 
        duration: 1500, 
        mask:true 
      }) 
 
    } 
  }, 
 
 
  //作业详情 
  goDetail(e){ 
    
    
    // var itemData = JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[e.currentTarget.dataset.index]); 
    console.log("作业详情4==="+this.data.dataList[e.currentTarget.dataset.index].taskId); 
    //已发布 
 //   if(this.data.selectIndex == 2){ 
      wx.navigateTo({ 
         url: '/packJumpTask/teacher/taskInfo/taskInfo?taskId='+this.data.dataList[e.currentTarget.dataset.index].taskId, 
      }) 
    // }else{ 
    //   //进行中、已结束 
    //   wx.navigateTo({ 
    //     url: '../../pages/record/detail?info='+JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[e.currentTarget.dataset.index]), 
    //   }) 
    // } 
    //console.log(this.data.selectIndex+"作业详情3==="+e.currentTarget.dataset.index); 
  }, 
 
  /** 
   * 生命周期函数--监听页面初次渲染完成 
   */ 
  onReady: function () { 
 
  }, 
 
  /** 
   * 生命周期函数--监听页面显示 
   */ 
  onShow: function () { 
    if(this.data.isRefresh == 1){ 
      console.log("列表个数数据 刷新数据"); 
      // var listData = this.data.switchList; 
      // listData[0].data.list = []; 
      // listData[1].data.list = []; 
      // listData[2].data.list = []; 
       
      this.setData({ 
          pageNo:1, 
          dataList:[],
          //switchList:listData, 
          totalPage:0 
      }) 
 
      this.showDate(); 
      this.getData(); 
    } 
    
  }, 
 
  /** 
   * 生命周期函数--监听页面隐藏 
   */ 
  onHide: function () { 
 
  }, 
 
  /** 
   * 生命周期函数--监听页面卸载 
   */ 
  onUnload: function () { 
 
  }, 
 
 
  // 加载更多 
  loadmore({ 
    detail 
  }) { 
    console.log('加载更多11='+this.data.pageSize * this.data.pageNo)
    if(this.data.pageSize * this.data.pageNo >= this.data.totalPage){
      wx.showToast({
        title: '暂无更多记录！',
        icon: 'none',
      })
      this.setData({
        isLoading:false
      })

      detail.success();

      return
    }

    if(this.data.isLoading){ 
      return 
    } 
 
    this.setData({ 
      pageNo:this.data.pageNo + 1 
    }) 
    this.getData(()=>{ 
      detail.success(); 
    }) 
  }, 
  // 刷新 
  refresh({ 
    detail 
  }) { 
    console.log('下拉刷新1='+this.data.pageSize * this.data.pageNo) 

    this.setData({ 
      pageNo:1, 
      dataList:[], 
      totalPage:0, 
      loading: false, 
      allloaded: false, 
    }) 
    this.getData(()=>{ 
      detail.success(); 
    }) 
  }, 
 
 
 
 
  /** 
   * 用户点击右上角分享 
   */ 
  onShareAppMessage: function () { 
 
  } 
})